<template>
  <el-dialog
    :title="!dataForm.courseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="教师编号" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="教师编号"></el-input>
    </el-form-item>
    <el-form-item label="属性" prop="attribute">
      <el-input v-model="dataForm.attribute" placeholder="属性"></el-input>
    </el-form-item>
    <el-form-item label="封面" prop="faceImg">
      <el-input v-model="dataForm.faceImg" placeholder="封面"></el-input>
    </el-form-item>
    <el-form-item label="课程简介" prop="info">
      <el-input v-model="dataForm.info" placeholder="课程简介"></el-input>
    </el-form-item>
    <el-form-item label="课时" prop="courseTime">
      <el-input v-model="dataForm.courseTime" placeholder="课时"></el-input>
    </el-form-item>
    <!-- <el-form-item label="添加时间" prop="addTime">
      <el-input v-model="dataForm.addTime" placeholder="添加时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          courseId: 0,
          name: '',
          teacherId: '',
          attribute: '',
          faceImg: '',
          addTime: '',
          info: '',
          courseTime: 0
        },
        dataRule: {
          name: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          teacherId: [
            { required: true, message: '教师编号不能为空', trigger: 'blur' }
          ],
          attribute: [
            { required: true, message: '属性不能为空', trigger: 'blur' }
          ],
          faceImg: [
            { required: true, message: '封面不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          info: [
            { required: true, message: '添加课程简介不能为空', trigger: 'blur' }
          ],
          courseTime: [
            { required: true, message: '添加课时不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.courseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.courseId) {
            this.$http({
              url: this.$http.adornUrl(`/wx/course/info/${this.dataForm.courseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.course.name
                this.dataForm.teacherId = data.course.teacherId
                this.dataForm.attribute = data.course.attribute
                this.dataForm.faceImg = data.course.faceImg
                this.dataForm.addTime = data.course.addTime
                this.dataForm.info = data.course.info
                this.dataForm.courseTime = data.course.courseTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wx/course/${!this.dataForm.courseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'courseId': this.dataForm.courseId || undefined,
                'name': this.dataForm.name,
                'teacherId': this.dataForm.teacherId,
                'attribute': this.dataForm.attribute,
                'faceImg': this.dataForm.faceImg,
                'addTime': this.dataForm.addTime,
                'info': this.dataForm.info,
                'courseTime': this.dataForm.courseTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
